package com.iot08.api.service;

import com.iot08.api.vo.request.base.BaseAccountReqVO;
import com.iot08.common.entity.app.BloodPressureRecord;
import com.iot08.common.entity.app.DeviceLastMissedVideoCall;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.DateUtil;
import com.iot08.common.utils.MessageUtils;
import com.iot08.common.utils.response.ResponseMap;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @version 1.0
 * @Description:
 * @author: Rock
 * @time: 2021/3/25 14:43
 */
@Slf4j
@Service
public class DeviceLastMissedVideoCallService extends BaseService<DeviceLastMissedVideoCall> {

    public DeviceLastMissedVideoCall add(String id, String imei, String videoId, String devName, String startTimeShowStr) {
        DeviceLastMissedVideoCall record = new DeviceLastMissedVideoCall();
        record.setId(id);
        record.setVideoId(videoId);
        record.setDevName(devName);
        record.setStartTimeShowStr(startTimeShowStr);
        record.setImei(imei);
        record.setCreatTime(System.currentTimeMillis() / 1000);
        record.setExpiryAt(DateUtil.getSystemExpiryAt());
        return this.save(record);
    }

    /**
     * @param vo
     * @time: 2021/3/25 15:44
     * @author: Rock
     * @version 1.0
     */
    public ResponseMap getLastMissedVideoCall(BaseAccountReqVO vo) {
        ResponseMap res = ResponseMap.success();
        res.put("haveData", 0);
        DeviceLastMissedVideoCall call = this.findById(vo.getOpenid());
        if (call != null) {
            if (call.getCreatTime() != null && System.currentTimeMillis() / 1000 - call.getCreatTime() < 10) {
                //如果时间间隔小于10s 的返回正常的情况
                return res;
            }
            res.put("haveData", 1);
            res.put("video_id", call.getVideoId());
            res.put("msg", MessageUtils.getLocale("API.request.video.missCallDes", call.getDevName(), call.getStartTimeShowStr()));
            this.delById(vo.getOpenid());
        }
        return res;
    }

    /**
     * 根据openid和videoid获取未接视频通话记录
     *
     * @param openid  用户openid
     * @param videoId 视频ID
     * @return DeviceLastMissedVideoCall 查询结果
     */
    public DeviceLastMissedVideoCall findByOpenidAndVideoId(String openid, String videoId) {
        Query query = new Query(Criteria.where("_id").is(openid).and("video_id").is(videoId));
        return this.findOne(query);
    }

    /**
     * 根据openid和videoid删除未接视频通话记录
     *
     * @param openid  用户openid
     * @param videoId 视频ID
     * @return boolean 删除是否成功
     */
    public boolean delByOpenidAndVideoId(String openid, String videoId) {
        Query query = new Query(Criteria.where("_id").is(openid).and("video_id").is(videoId));
        DeleteResult result = mongoTemplate.remove(query, getTClass());
        return result != null && result.getDeletedCount() > 0;
    }
}
